package initialize

import (
	"bytes"
	_ "embed"
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/global"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/global/config"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/tools/zaps"
	"github.com/spf13/viper"
	"sync"
)

var (
	zapInitOnce sync.Once
)

//go:embed zap-config.yaml
var zapCfg []byte

// InitZap  初始化zap日志，已做重复处理
func InitZap() {
	zapInitOnce.Do(func() {
		initZap0()
	})
}
func initZap0() {
	vp := viper.New()
	//开始读取环境变量，如果没有调用这个函数，则下面无法读取到path的值
	vp.AutomaticEnv()
	vp.SetConfigType("yaml")
	vp.ReadConfig(bytes.NewReader(zapCfg))
	vp.WatchConfig()

	if err := vp.Unmarshal(&config.TotalConfig); err != nil {
		panic(fmt.Sprintf("initZap0 err:%v", err))
	}

	fmt.Printf("-----initZap0-----%+v \n", config.TotalConfig.Zap)

	global.ZapLog = zaps.CreateZapFactory(config.TotalConfig.Zap, zaps.ZapLogHandler)

}
